package com.yf.exam.modules.cert.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.exam.core.api.ApiRest;
import com.yf.exam.core.api.controller.BaseController;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.core.api.dto.BaseIdRespDTO;
import com.yf.exam.core.api.dto.BaseIdsReqDTO;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.modules.cert.dto.CertDTO;
import com.yf.exam.modules.cert.dto.request.CertReqDTO;
import com.yf.exam.modules.cert.dto.response.CertRespDTO;
import com.yf.exam.modules.cert.entity.Cert;
import com.yf.exam.modules.cert.service.CertService;
import com.yf.exam.modules.qu.dto.request.QuRepoBatchReqDTO;
import com.yf.exam.modules.qu.service.QuRepoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* <p>
* 证书控制器
* </p>
*
* @author 聪明笨狗
* @since 2020-05-25 13:25
*/
@Api(tags={"证书"})
@RestController
@RequestMapping("/exam/api/cert")
public class CertController extends BaseController {

    @Autowired
    private CertService baseService;

    @Autowired
    private QuRepoService quRepoService;

    /**
    * 添加或修改
    * @param reqDTO
    * @return
    */
    @RequiresRoles("sa")
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiRest<BaseIdRespDTO> save(@RequestBody CertDTO reqDTO) {
        String certId = baseService.save(reqDTO);
        return super.success(new BaseIdRespDTO(certId));
    }

    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @RequiresRoles("sa")
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
    * 查找详情
    * @param reqDTO
    * @return
    */
    @RequiresRoles("sa")
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public ApiRest<CertDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        Cert entity = baseService.getById(reqDTO.getId());
        CertDTO dto = new CertDTO();
        BeanUtils.copyProperties(entity, dto);
        return super.success(dto);
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @RequiresRoles("sa")
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<CertRespDTO>> paging(@RequestBody PagingReqDTO<CertReqDTO> reqDTO) {

        //分页查询并转换
        IPage<CertRespDTO> page = baseService.paging(reqDTO);

        return super.success(page);
    }

    /**
     * 批量操作
     * @param reqDTO
     * @return
     */
    @RequiresRoles("sa")
    @ApiOperation(value = "批量操作", notes = "批量加入或从证书移除")
    @RequestMapping(value = "/batch-action", method = { RequestMethod.POST})
    public ApiRest batchAction(@RequestBody QuRepoBatchReqDTO reqDTO) {

        //分页查询并转换
        quRepoService.batchAction(reqDTO);
        return super.success();
    }

    /**
     * 获取所有证书列表
     * @return
     */
    @RequiresRoles("sa")
    @ApiOperation(value = "获取所有证书列表", notes = "用于考试创建时选择证书模板")
    @RequestMapping(value = "/list", method = { RequestMethod.GET})
    public ApiRest<List<CertDTO>> list() {
        List<CertDTO> certList = baseService.listAll();
        return super.success(certList);
    }

    /**
     * 获取证书关联的考试列表
     * @param certId 证书ID
     * @return
     */
    @RequiresRoles("sa")
    @ApiOperation(value = "获取证书关联的考试列表")
    @RequestMapping(value = "/exams/{certId}", method = { RequestMethod.GET})
    public ApiRest<List<Object>> getExamsByCertId(@PathVariable String certId) {
        List<Object> examList = baseService.getExamsByCertId(certId);
        return super.success(examList);
    }
}
